1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package com.touchgraph.graphlayout.graphelements;
51
52 import java.util.*;
53
54 import com.touchgraph.graphlayout.*;
55
56 /*** GESUtils is a set of functions that return information about a GraphEltSet
57 *
58 * @author Alexander Shapiro
59 * @version 1.21 $Id: GESUtils.java,v 1.1.1.1 2004/02/06 08:44:05 keesj Exp $
60 */
61 public class GESUtils {
62
63 /*** Returns a hashtable of Node-Integer pairs, where integers are the minimum
64 * distance from the focusNode to any given Node, and the Nodes in the Hashtable
65 * are all within radius distance from the focusNode.
66 *
67 * Nodes with edge degrees of more then maxAddEdgeCount are not added to the hashtable, and those
68 * with edge degrees of more then maxExpandEdgeCount are added but not expanded.
69 *
70 * If unidirectional is set to true, then edges are only follewed in the forward direction.
71 *
72 */
73
74 public static Hashtable calculateDistances(GraphEltSet ges, Node focusNode, int radius,
75 int maxAddEdgeCount, int maxExpandEdgeCount,
76 boolean unidirectional ) {
77 Hashtable distHash = new Hashtable();
78 distHash.put(focusNode,new Integer(0));
79
80 TGNodeQueue nodeQ = new TGNodeQueue();
81 nodeQ.push(focusNode);
82
83 while (!nodeQ.isEmpty()) {
84 Node n = nodeQ.pop();
85 int currDist = ((Integer) distHash.get(n)).intValue();
86
87 if (currDist>=radius) break;
88
89 for (int i = 0 ; i < n.edgeCount(); i++) {
90 Edge e=n.edgeAt(i);
91 if(n!=n.edgeAt(i).getFrom() && unidirectional) continue;
92 Node adjNode = e.getOtherEndpt(n);
93 if(ges.contains(e) && !distHash.containsKey(adjNode) && adjNode.edgeCount()<=maxAddEdgeCount) {
94 if (adjNode.edgeCount()<=maxExpandEdgeCount) nodeQ.push(adjNode);
95 distHash.put(adjNode,new Integer(currDist+1));
96 }
97 }
98 }
99 return distHash;
100 }
101
102 public static Hashtable calculateDistances(GraphEltSet ges, Node focusNode, int radius ) {
103 return calculateDistances(ges, focusNode, radius, 1000, 1000, false);
104 }
105
106 /*** A temporary function that returns the largest connected subgraph in a GraphEltSet.
107 */
108
109 public static Collection getLargestConnectedSubgraph(GraphEltSet ges) {
110 int nodeCount = ges.nodeCount();
111 if(nodeCount==0) return null;
112
113 Vector subgraphVector = new Vector();
114 for(int i=0; i<nodeCount; i++) {
115 Node n = ges.nodeAt(i);
116 boolean skipNode=false;
117 for (int j = 0; j<subgraphVector.size();j++) {
118 if(((Collection) subgraphVector.elementAt(j)).contains(n)) {
119 skipNode=true;
120 }
121 }
122 Collection subgraph = calculateDistances(ges,n,1000).keySet();
123 if(subgraph.size()>nodeCount/2) return subgraph;
124 if (!skipNode) subgraphVector.add(subgraph);
125
126 }
127
128 int maxSize=0;
129 int maxIndex=0;
130 for (int j = 0; j<subgraphVector.size();j++) {
131 if(((Collection) subgraphVector.elementAt(j)).size()>maxSize) {
132 maxSize = ((Collection) subgraphVector.elementAt(j)).size();
133 maxIndex = j;
134 }
135 }
136
137 return (Collection) subgraphVector.elementAt(maxIndex);
138 }
139
140 }